package com.example.test01.mysql_test.web;

import com.alibaba.excel.EasyExcel;
import com.example.test01.mysql_test.entity.StudentEntity;
import com.example.test01.mysql_test.mapper.StudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 导出
 *
 * Author: duende
 * DateTime: 2022/4/17 20:01
 */
@RestController
public class ExportController {

    @Autowired
    private StudentMapper studentMapper;

    /**
     * 导出表格
     */
    @RequestMapping("/exportTable")
    public void exportTable(HttpServletResponse response, HttpServletRequest request) throws IOException {
        //请求头
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");

        //所有数据
//        List<StudentEntity> list = null ; // = recordCarAllService.exporztTable();
        List<StudentEntity> list = studentMapper.queryStudent();
        //获取输出流
        OutputStream outputStream = response.getOutputStream();

        try {
            //要导出的字段
            Set<String> setTable = new HashSet<>();
            setTable.add("name");
            setTable.add("yuwen");
            setTable.add("shuxue");
            setTable.add("yingyu");
            setTable.add("zonghe");
            //导出
            EasyExcel.write(outputStream, StudentEntity.class).includeColumnFiledNames(setTable).sheet().doWrite(list);
            outputStream.flush();
        } catch (IOException e) {
        } finally {
            outputStream.close();
        }
    }


}
